import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import store from '@/store/store'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)
Vue.config.productionTip = false

// 注册一个路由全局守卫，作用是在路由跳转前，对路由进行判断，防止未登录的用户跳转到其他需要登录的页面
router.beforeEach((to,from,next)=> {
  let token = localStorage.getItem('mytoken')
  if(token) {
    next()
  } else {
    if(to.path!=='/login') {
      // 没有登录且去的页面是需要登录才能操作的页面，就跳转去登录页面
      next({path:'/login'})
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
